package com.hama.iotrust.common.mybatis.result;

import java.util.List;

import com.hama.iotrust.common.Constants;
import com.hama.iotrust.common.mybatis.criteria.Criteria;

/**
 * 分页对象.
 * 
 * @param <E> 分页内结果集对象的类型
 * @author 金建强(ptma@163.com)
 * @version 1.0.0
 * @since 2013-4-2 19:58:35
 */
public class Pager<E> implements java.io.Serializable {

    private static final long serialVersionUID = -606035128542576010L;

    private int               totalRecords;
    private int               totalPages;
    private int               pageSize         = Constants.DEFAULT_PAGESIZE;
    private int               currentPageNo;
    private List<E>           list;

    /**
     * 分页对象构造函数.
     * 
     * @param criteria 分页查询条件
     */
    public Pager(Criteria<E> criteria){
        this(criteria.getCurrentPage(), criteria.getPageSize(), criteria.getTotalRecords(), null);
    }

    /**
     * 分页对象构造函数.
     * 
     * @param criteria 分页查询条件
     * @param list 分页结果集
     */
    public Pager(Criteria<E> criteria, List<E> list){
        this(criteria.getCurrentPage(), criteria.getPageSize(), criteria.getTotalRecords(), list);
    }

    /**
     * 分页对象构造函数.
     * 
     * @param currentPageNo 当前页码
     * @param pageSize 页尺寸
     * @param totalRecords 总记录数
     */
    public Pager(int currentPageNo, int pageSize, int totalRecords){
        this(currentPageNo, pageSize, totalRecords, null);
    }

    /**
     * 分页对象构造函数.
     * 
     * @param currentPageNo 当前页码
     * @param pageSize 页尺寸
     * @param totalRecords 总记录数
     * @param list 分页结果集
     */
    public Pager(int currentPageNo, int pageSize, int totalRecords, List<E> list){
        this.currentPageNo = currentPageNo;
        this.pageSize = pageSize;
        this.totalRecords = totalRecords;

        if (currentPageNo < 1) {
            this.currentPageNo = 1;
        }

        this.totalPages = (this.totalRecords + pageSize - 1) / pageSize;

        if (currentPageNo > totalPages) {
            this.currentPageNo = totalPages;
        }
        this.list = list;
    }

    public int getTotalRecords() {
        return totalRecords;
    }
    
    
    
    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getCurrentPageNo() {
        return currentPageNo;
    }

    public void setCurrentPageNo(int currentPageNo) {
        this.currentPageNo = currentPageNo;
    }
    
    
    public int getStartRow() {
    	 int offset = (currentPageNo - 1) * getPageSize();
    	 return offset;
    }
    
    public List<E> getList() {
        return list;
    }

    public void setList(List<E> list) {
        this.list = list;
    }
}
